package Practice.Practice_02;

import java.util.Scanner;

public class Demo03 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("输入数组：");
        String str = sc.nextLine();
        String s = trim(str);
        System.out.println(s);
    }

    public static String trim(String str){
        String newstr = str.trim();

        for(int i = newstr.length()-1; i >= 0; i--) {
            if(newstr.charAt(i) != ' ') {
                break;
            }
            newstr = newstr.replaceAll(" +","");
        }

        for(int i = 0; i < newstr.length(); i++) {
            if(newstr.charAt(i) != ' ') {
                break;
            }
            newstr = newstr.replaceAll(" +","");
            i--;
        }

        return newstr;
    }
}
